/*
 * Project it.battlehorse.rcp.tools.log
 * Created on Nov 19, 2005
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * A copy of the LGPL is available also at
 * http://www.gnu.org/copyleft/lesser.html
 * 
 */ 
package it.battlehorse.rcp.tools.log;

import it.battlehorse.rcp.tools.commons.EventType;
import it.battlehorse.rcp.tools.commons.Severity;

import java.util.ArrayList;
import java.util.List;

/**
 * Provides a basic implementation for a loggable item
 * 
 * @author battlehorse
 * @since Nov 19, 2005
 */
public class BasicLoggable implements ILoggable {
	
	/**
	 * The log item severity
	 */
	public Severity severity;
	
	/**
	 * The log item time
	 */
	public long time;
	
	/**
	 * The log item source
	 */
	public Object source;
	
	/**
	 * The log item type
	 */
	public EventType type;
	
	/**
	 *  The log message
	 */
	public String message;
	
	/**
	 * The sub log elements
	 */
	private List<ILoggable> subLogs;
	
	/**
	 * The parent loggable
	 */
	private ILoggable parent;
	
	/**
	 * The throwable 
	 */
	private Throwable throwable;
	
	/**
	 * Creates a new instance of the class
	 * 
	 * @param message the log message
	 * @param source the log source
	 */
	public BasicLoggable(String message, Object source) {
		this(message,null,null,source);
	}
	
	/**
	 * Creates a new instance of the class
	 *  
	 * @param message the log message
	 * @param severity the log severity
	 * @param source the log source
	 */
	public BasicLoggable(String message, Severity severity, Object source) {
		this(message, severity, null,source);
	}	
	
	/**
	 * Creates a new instance of the class
	 * 
	 * @param message the log message
	 * @param severity the log severity
	 * @param type the log type
	 * @param source the log source
	 */
	public BasicLoggable(
			String message, 
			Severity severity, 
			EventType type,
			Object source) {
		
		this.message = message;
		this.severity = severity;
		this.type = type;
		this.source = source;
		this.time = System.currentTimeMillis();
		
		this.subLogs = new ArrayList<ILoggable>();
		this.parent = null;
		this.throwable = null;
	}
	
	/**
	 * Creates a new instance of the class. The loggable item created with this constructor is used
	 * to represent an application error or exception and its severity and event type are respectively
	 * assigned as {@code Severity.ERROR}, and {@code EventType.EXCEPTION_EVENT} .  
	 * 
	 * @param message the log message
	 * @param exception the exception to be logged
	 * @param source the log source
	 */
	public BasicLoggable(
			String message,
			Throwable exception,
			Object source) {
		this(message, Severity.ERROR, EventType.EXCEPTION_EVENT, source);
		this.throwable = exception;
	}
	
	/**
	 * Adds a sub log element to this log event. If the sublog is instance of {@code BasicLoggable}
	 * then for its parent is set to be this element.
	 * 
	 * @param sub the sub log element to be added
	 */
	public void addSubLog(ILoggable sub) {
		subLogs.add(sub);
		if (sub instanceof BasicLoggable) {
			((BasicLoggable) sub).setParentLog(this);
		}
	}
	
	/**
	 * Sets the parent log
	 * 
	 * @param loggable the parent log
	 */
	void setParentLog(ILoggable loggable) {
		this.parent = loggable;
	}

	/* (non-Javadoc)
	 * @see it.battlehorse.rcp.tools.log.ILoggable#getSeverity()
	 */
	public Severity getSeverity() {
		return severity;
	}

	/* (non-Javadoc)
	 * @see it.battlehorse.rcp.tools.log.ILoggable#getTime()
	 */
	public long getTime() {
		return time;
	}

	/* (non-Javadoc)
	 * @see it.battlehorse.rcp.tools.log.ILoggable#getSource()
	 */
	public Object getSource() {
		return source;
	}

	/* (non-Javadoc)
	 * @see it.battlehorse.rcp.tools.log.ILoggable#getType()
	 */
	public EventType getType() {
		return type;
	}

	/* (non-Javadoc)
	 * @see it.battlehorse.rcp.tools.log.ILoggable#getMessage()
	 */
	public String getMessage() {
		return message;
	}

	/* (non-Javadoc)
	 * @see it.battlehorse.rcp.tools.log.ILoggable#getSubLogs()
	 */
	public List<ILoggable> getSubLogs() {
		return subLogs;
	}
	
	/*
	 * (non-Javadoc)
	 * @see it.battlehorse.rcp.tools.log.ILoggable#getParentLog()
	 */
	public ILoggable getParentLog() {		
		return parent;
	}
	
	/*
	 * (non-Javadoc)
	 * @see it.battlehorse.rcp.tools.log.ILoggable#getThrowable()
	 */
	public Throwable getThrowable() {
		return throwable;
	}

}
